package top.wherewego.vnt.fragment;

import static android.app.Activity.RESULT_OK;

import android.content.Intent;
import android.net.VpnService;
import android.view.View;

import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;

import com.kongzue.baseframework.BaseBindingFragment;
import com.kongzue.dialogx.dialogs.BottomMenu;
import com.kongzue.dialogx.interfaces.OnMenuItemClickListener;

import java.util.ArrayList;
import java.util.List;

import top.wherewego.vnt.APP;
import top.wherewego.vnt.Service.VntService;
import top.wherewego.vnt.activity.MainActivity;
import top.wherewego.vnt.bean.Conf;
import top.wherewego.vnt.databinding.FragHomeBinding;
import top.wherewego.vnt.utils.DataHelper;

public class HomeFragment extends BaseBindingFragment<MainActivity, FragHomeBinding> {
    private int lastConfIndex = -1;
    private ActivityResultLauncher<Intent> vpnLauncher;

    @Override
    public void initDatas() {

    }

    @Override
    public void setEvents() {
        vpnLauncher = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(),
                result -> {
                    if (result.getResultCode() == RESULT_OK) {
                        // 用户已经授予所需权限，启动 VPN 服务
                    }
                });
    }

    @Override
    public void onShow(boolean isSwitchFragment) {
        super.onShow(isSwitchFragment);
        lastConfIndex = DataHelper.getIntergerSF(me, "lastConfIndex");
        if (APP.getConfList().isEmpty()) {
            binding.homeConfName.setText("暂无配置,请添加");
            lastConfIndex = -1;
            DataHelper.setIntergerSF(me, "lastConfIndex", lastConfIndex);
            return;
        }
        if (lastConfIndex == -1 || (lastConfIndex > APP.getConfList().size() - 1)) {
            lastConfIndex = 0;
            DataHelper.setIntergerSF(me, "lastConfIndex", lastConfIndex);
        }
        binding.homeConfName.setText("当前配置 : " + APP.getConfList().get(lastConfIndex).getConName());
        binding.homeConfName.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //点击显示配置列表
                List<CharSequence> titles = new ArrayList<>();
                for (Conf conf : APP.getConfList()) {
                    titles.add(conf.getConName());
                }
                BottomMenu.show(titles)
                        .setOnMenuItemClickListener(new OnMenuItemClickListener<BottomMenu>() {
                            @Override
                            public boolean onClick(BottomMenu dialog, CharSequence text, int index) {
                                lastConfIndex = index;
                                DataHelper.setIntergerSF(me, "lastConfIndex", lastConfIndex);
                                binding.homeConfName.setText("当前配置 : " + APP.getConfList().get(lastConfIndex).getConName());
                                return false;
                            }
                        });
            }
        });
        binding.homeConnect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                log("点击链接");
//                Intent vpnIntent = VntService.prepare(me);
//                vpnLauncher.launch(vpnIntent);
//                if (vpnIntent != null) {
//                    startActivityForResult(vpnIntent, 0);
//                } else {
//                    me.startService(new Intent(me, VntService.class));
//                }

            }
        });
    }
}
